﻿using System;
using Fusion.States;
using Client.Game_States.Game_GUI_s;
using Fusion.Content;
using Fusion.Management;
using Microsoft.Xna.Framework;
using Lidgren.Network;
using EclipseLib.Shared;

namespace Client.Game_States
{
    public class RegisterMenu : BaseState
    {
        // Constructor
        public RegisterMenu() { }

        // Data Members
        RegMenuGUI GUI;
        GameTexture Logo, Background;
        ClientSocket.ReceiveData NetworkDataFunc;

        // State Methods
        public override void Enter()
        {
            GUI = new RegMenuGUI();

            Logo = GFXHelper.LoadInterfaceTexture("Logo");
            Background = GFXHelper.LoadInterfaceTexture("Background");

            NetworkDataFunc = new ClientSocket.ReceiveData(IncomingPacket);
            Network.Socket.OnReceiveData += NetworkDataFunc;
        }
        public override void Exit()
        {
            GUI.Dispose();

            Logo.Release();
            Background.Release();

            Network.Socket.OnReceiveData -= NetworkDataFunc;
        }
        public override bool Input(float Delta)
        {
            return true;
        }
        public override void Update(float Delta)
        {
            GUI.Update();
        }
        public override void Render(float Delta)
        {
            FCanvas.Begin();
            {
                FCanvas.Draw(Background, Vector2.Zero, Color.White);

                FCanvas.Draw(Logo, new Vector2(GUI.RegWindow.Bounds.Location.X + 17, 150f), Color.Black);
                FCanvas.Draw(Logo, new Vector2(GUI.RegWindow.Bounds.Location.X + 15, 150f), Color.Black);
                FCanvas.Draw(Logo, new Vector2(GUI.RegWindow.Bounds.Location.X + 16, 151f), Color.Black);
                FCanvas.Draw(Logo, new Vector2(GUI.RegWindow.Bounds.Location.X + 16, 149f), Color.Black);
                FCanvas.Draw(Logo, new Vector2(GUI.RegWindow.Bounds.Location.X + 16, 150f), Color.White);

                // Controls.Render(Delta);
                GUI.Render();
            }
            FCanvas.End();
        }

        // Network Methods
        private void IncomingPacket(NetIncomingMessage Msg)
        {
            switch (Msg.MessageType)
            {
                case NetIncomingMessageType.Data:
                    {
                        ServerPackets Packet = (ServerPackets)Msg.ReadInt32();

                        switch (Packet)
                        {
                            case ServerPackets.SendErrorMessage: GUI.ShowError(Msg.ReadString()); break;
                            case ServerPackets.SendRegistrationResults: GUI.ShowError(Msg.ReadString()); break;
                        }
                    }
                    break;
            }
        }
    }
}
